package com.momo.todo.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.momo.todo.config.SqliteConfig;
import com.momo.todo.model.HistoryModel;
import com.momo.todo.model.PlanModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2016/10/29.
 * 测试
 */

public class SqlUtils {

    private Context context;
    private MyDatabaseHelper databaseHelper;
    private SQLiteDatabase dbWriter;
    private SQLiteDatabase dbReader;

    public SqlUtils(Context context, String databaseName) {
        databaseHelper = new MyDatabaseHelper(context, databaseName);
        this.context = context;
    }

    public void savePlan(PlanModel model) {
        dbWriter = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        int type = model.getType();
        String content = model.getContent();
        long time = model.getTime();
        int isTop;
        if (model.isTop()) {
            isTop = 1;
        } else {
            isTop = 0;
        }

        values.put("type", type);
        values.put("time", time);
        values.put("content", content);
        values.put("isTop", isTop);
        dbWriter.insert(SqliteConfig.PLAN_TABLE_NAME, null, values);
        values.clear();
        dbWriter.close();
        LogUtils.log("插入成功!" + model.toString()+"time:" + model.getTime());
    }

    public List<PlanModel> getPlanList() {
        List<PlanModel> planList = new ArrayList();
        dbReader = databaseHelper.getReadableDatabase();
        Cursor cursor = dbReader.query(SqliteConfig.PLAN_TABLE_NAME, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                PlanModel model = new PlanModel();

                int type = cursor.getInt(cursor.getColumnIndex("type"));
                long time = cursor.getLong(cursor.getColumnIndex("time"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                int isTop = cursor.getInt(cursor.getColumnIndex("isTop"));

                model.setType(type);
                model.setTime(time);
                model.setContent(content);
                if (isTop == 1) {
                    model.setTop(true);
                } else {
                    model.setTop(false);
                }

                planList.add(model);

                LogUtils.log(model.toString());
            } while (cursor.moveToNext());
        }
        dbReader.close();
        Collections.reverse(planList);
        return planList;
    }

    public void delete(PlanModel model) {
        dbWriter = databaseHelper.getWritableDatabase();
        dbWriter.delete(SqliteConfig.PLAN_TABLE_NAME,"time = ?",new String[]{String.valueOf(model.getTime())});
        dbWriter.close();
        LogUtils.log("删除成功!"+"content:" + model.getContent());
    }

    public void update(PlanModel model){
        dbWriter = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        int type = model.getType();
        String content = model.getContent();

        int isTop;
        if (model.isTop()) {
            isTop = 1;
        } else {
            isTop = 0;
        }

        values.put("type", type);
        values.put("content", content);
        values.put("isTop", isTop);

        long time = model.getTime();
        dbWriter.update(SqliteConfig.PLAN_TABLE_NAME,values,"time = ?",new String[]{String.valueOf(time)});
        dbWriter.close();
        LogUtils.log("更新成功!"+ time);
    }

    public void saveHistoryItem(long finishtTime,PlanModel plan) {
        dbWriter = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        int type = plan.getType();
        String content = plan.getContent();
        long time = plan.getTime();

        values.put("type", type);
        values.put("time", time);
        values.put("content", content);
        values.put("finishTime",finishtTime);

        dbWriter.insert(SqliteConfig.HISTORY_TABLE_NAME, null, values);
        values.clear();
        dbWriter.close();
        LogUtils.log("插入成功!" + plan.toString());
    }

    public List<HistoryModel> getHistoryList(){
        List<HistoryModel> historyList = new ArrayList<>();

        dbReader = databaseHelper.getReadableDatabase();

        Cursor cursor = dbReader.query(SqliteConfig.HISTORY_TABLE_NAME, null, null, null, null, null, null);

        HistoryModel history;
        if (cursor.moveToFirst()) {
            do {
                long finishTime = cursor.getLong(cursor.getColumnIndex("finishTime"));
                int type = cursor.getInt(cursor.getColumnIndex("type"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                long time = cursor.getLong(cursor.getColumnIndex("time"));

                if(historyList.size()==0){
                    history = new HistoryModel();

                    history.setTime(finishTime);
                    PlanModel plan = new PlanModel();
                    plan.setTime(time);
                    plan.setType(type);
                    plan.setContent(content);
                    history.getPlanList().add(plan);

                    historyList.add(0,history);
                }else{
                    if(!TimeUtils.isSameDay(finishTime,historyList.get(0).getTime())){
                        history = new HistoryModel();

                        history.setTime(finishTime);
                        PlanModel plan = new PlanModel();
                        plan.setTime(time);
                        plan.setType(type);
                        plan.setContent(content);
                        history.getPlanList().add(0,plan);

                        historyList.add(0,history);
                    }else{
                        PlanModel plan = new PlanModel();
                        plan.setTime(time);
                        plan.setType(type);
                        plan.setContent(content);
                        historyList.get(0).getPlanList().add(0,plan);
                    }
                }
            } while (cursor.moveToNext());
        }
        dbReader.close();
        return historyList;
    }

}
